﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using BenevolentSunData;

namespace BenevolentSun
{

    class OptionScreen : Screen
    {

        TextField title;

        Sprite background;

        Sprite logo;

        InstructionsControl instructionsControl;

        HorizontalMenuControl horizontalMenuControl;

        DimmerControl dimmerControl;


        public OptionScreen()
        {
            this.TransitionOffTime = new TimeSpan(0, 0, 0, 0, 100);
        }


        public override void LoadContent()
        {
            SpriteFont titleFont = Content.Load<SpriteFont>("Fonts/Screen Title");
            title = new TextField("Options", titleFont, new Color(0, 255, 0));
            title.AlignH = AlignH.Center;
            title.Position = new Vector2(1280f / 2f, 10f);
            title.Color.A = 0;

            logo = new Sprite(Content.Load<Texture2D>("Textures/Gui/Logo"));

            background = new Sprite(Content.Load<Texture2D>("Textures/Gui/Background"));

            instructionsControl = new InstructionsControl(Content);

            SpriteFont horizontalMenuFont = Content.Load<SpriteFont>("Fonts/Horizontal Menu Font");
            horizontalMenuControl = new HorizontalMenuControl(Content);
            horizontalMenuControl.AddEntry(new TextField(MusicOptionText, horizontalMenuFont, new Color(255, 0, 255)));
            if (Settings.DeveloperOptionsEnabled)
                horizontalMenuControl.AddEntry(new TextField("Toggle Beam\nLights", horizontalMenuFont, new Color(255, 255, 255)));
            horizontalMenuControl.AddEntry(new TextField("Credits", horizontalMenuFont, new Color(0, 255, 255)));
            horizontalMenuControl.AddEntry(new TextField("Exit", horizontalMenuFont, new Color(255, 255, 0)));
            horizontalMenuControl.Initialize();
            horizontalMenuControl.FadeIn();

            dimmerControl = new DimmerControl(Content);
            dimmerControl.FadeIn();
            dimmerControl.AdvanceToEnd();

            refreshMenuInstructions();

            base.LoadContent();
        }


        public override void ExitScreen()
        {
            horizontalMenuControl.FadeOut();
            instructionsControl.FadeOutInstructions();
            base.ExitScreen();
        }



        public String MusicOptionText 
        {
            get 
            { 
                if (Session.IsMusicMuted)
                {
                    return "Music Off";
                } 
                else 
                {
                    return "Music On";
                }
            }
        }


        public override void HandleInput()
        {
            if (ActionMap.Cancel || ActionMap.Start)
            {
                ScreenManager.AddScreen(new SaveProfileScreen());
                ExitScreen();
            }
            else
            {
                if (ActionMap.Right)
                {
                    if (horizontalMenuControl.Next())
                    {
                        refreshMenuInstructions();
                        AudioManager.PlayCue("Pop");
                    }
                }

                if (ActionMap.Left)
                {
                    if (horizontalMenuControl.Prev())
                    {
                        refreshMenuInstructions();
                        AudioManager.PlayCue("Pop");
                    }
                }

                if (ActionMap.Confirm)
                {
                    if (horizontalMenuControl.Selected.Text == "Music On" || horizontalMenuControl.Selected.Text == "Music Off")
                    {
                        Session.IsMusicMuted = !Session.IsMusicMuted;
                        horizontalMenuControl.Selected.Text = MusicOptionText;
                        refreshMenuInstructions();
                    }
                    else if (horizontalMenuControl.Selected.Text == "Toggle Beam\nLights")
                    {
                        Settings.DisableBeamLighting = !Settings.DisableBeamLighting;
                        refreshMenuInstructions();
                    }
                    else if (horizontalMenuControl.Selected.Text == "Credits")
                    {
                        ScreenManager.AddScreen(new CreditsScreen());
                    }
                    else if (horizontalMenuControl.Selected.Text == "Exit")
                    {
                        ScreenManager.AddScreen(new SaveProfileScreen());
                        ExitScreen();
                    }
                }
            }

            base.HandleInput();
        }


        private void refreshMenuInstructions()
        {
            if (horizontalMenuControl.Selected.Text == "Music On")
            {
                instructionsControl.HideInstructions();
                instructionsControl.SetInstructions("Turn the music off.");
                instructionsControl.FadeInInstructions();
            }
            else if (horizontalMenuControl.Selected.Text == "Music Off")
            {
                instructionsControl.HideInstructions();
                instructionsControl.SetInstructions("Turn the music on.");
                instructionsControl.FadeInInstructions();
            }
            else if (horizontalMenuControl.Selected.Text == "Toggle Beam\nLights")
            {
                instructionsControl.HideInstructions();
                instructionsControl.SetInstructions("Enabled: " + !Settings.DisableBeamLighting);
                instructionsControl.FadeInInstructions();
            }
            else if (horizontalMenuControl.Selected.Text == "Credits")
            {
                instructionsControl.HideInstructions();
                instructionsControl.SetInstructions("Who is responsible for this anyways?");
                instructionsControl.FadeInInstructions();
            }
            else if (horizontalMenuControl.Selected.Text == "Exit")
            {
                instructionsControl.HideInstructions();
                instructionsControl.SetInstructions("That's enough Options configuration for one day.");
                instructionsControl.FadeInInstructions();
            }
        }


        public override void Update(GameTime gameTime, bool isFocusAvailable, bool isOccluded)
        {
            instructionsControl.Update(gameTime);
            horizontalMenuControl.Update(gameTime);
            dimmerControl.Update(gameTime);

            title.Color.A = (byte)horizontalMenuControl.FadeValue;

            base.Update(gameTime, isFocusAvailable, isOccluded);
        }


        public override void Draw(GameTime gameTime)
        {
            /*
            SpriteBatch.Begin();
            menu.Draw(SpriteBatch);
            SpriteBatch.End();
            */

            SpriteBatch.Begin();

            background.Draw(SpriteBatch);

            logo.Draw(SpriteBatch);

            dimmerControl.Draw(SpriteBatch);

            horizontalMenuControl.Draw(SpriteBatch);

            instructionsControl.Draw(SpriteBatch);

            title.Draw(SpriteBatch);

            SpriteBatch.End();

            base.Draw(gameTime);
        }

    }
}
